package com.yiwei.zhh.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.util.Base64;


/**
 * SharedPreferences 工具类
 * Created by Master.Dong on 2016/4/6 0006.
 * 取值时只用传入context和对应的key，就能获取到对应的value；
 * 设值时，也是传入context和对应key和value即可
 */
public class PreferenceUtils {
    private static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(Const.SP_NAME, Context.MODE_PRIVATE);
    }

    /**
     * 本地文件存储 键值对
     */
    public static String getString(Context context, String key,
                                   String defaultValue) {
        SharedPreferences settings = getSharedPreferences(context);
        String value = settings.getString(Base64.encode(key.getBytes()),
                defaultValue);
        if (value.equals("0")) {
            return "0";
        } else {
            return value;
        }

    }

    public static void putString(Context context, String key, String value) {
        SharedPreferences settings = getSharedPreferences(context);
        settings.edit().putString(Base64.encode(key.getBytes()), value).commit();
    }

    public static boolean getBoolean(Context context, String key,
                                     boolean defaultValue) {
        SharedPreferences settings = getSharedPreferences(context);
        return settings.getBoolean(Base64.encode(key.getBytes()), defaultValue);
    }

    public static void putBoolean(Context context, String key, boolean value) {
        SharedPreferences settings = getSharedPreferences(context);
        settings.edit().putBoolean(Base64.encode(key.getBytes()), value)
                .commit();
    }

    public static void putInt(Context context, String key, int value) {
        SharedPreferences settings = getSharedPreferences(context);
        settings.edit().putInt(Base64.encode(key.getBytes()), value).commit();
    }

    public static int getInt(Context context, String key, int defaultValue) {
        SharedPreferences settings = getSharedPreferences(context);
        return settings.getInt(Base64.encode(key.getBytes()), defaultValue);
    }

    public static void putFloat(Context context, String key, float value) {
        SharedPreferences settings = getSharedPreferences(context);
        settings.edit().putFloat(Base64.encode(key.getBytes()), value).commit();
    }

    public static float getFloat(Context context, String key, float defaultValue) {
        SharedPreferences settings = getSharedPreferences(context);
        return settings.getFloat(Base64.encode(key.getBytes()), defaultValue);
    }

    public static void putLong(Context context, String key, long value) {
        SharedPreferences settings = getSharedPreferences(context);
        settings.edit().putLong(Base64.encode(key.getBytes()), value).commit();
    }

    public static long getLong(Context context, String key, long defaultValue) {
        SharedPreferences settings = getSharedPreferences(context);
        return settings.getLong(Base64.encode(key.getBytes()), defaultValue);
    }



    public static double getDouble(Context context, String key,
                                   double defaultValue) {
        SharedPreferences settings = getSharedPreferences(context);
        String value = settings.getString(Base64.encode(key.getBytes()), defaultValue+"");
        return Double.parseDouble(value);
    }

    public static void putDouble(Context context, String key, double value) {
        SharedPreferences settings = getSharedPreferences(context);
        settings.edit().putString(Base64.encode(key.getBytes()), Double.toString(value)).commit();
    }

    public static void remove(Context context, String key) {
        SharedPreferences settings = getSharedPreferences(context);
        settings.edit().remove(Base64.encode(key.getBytes())).commit();
    }

    public static void clear(Context context, SharedPreferences p) {
        SharedPreferences.Editor editor = p.edit();
        editor.clear();
        editor.commit();
    }

    public static boolean hasKey(Context context, String key) {
        return getSharedPreferences(context).contains(
                Base64.encode(key.getBytes()));
    }
}
